package com.guli.edu.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.common.vo.R;
import com.guli.edu.entity.Course;
import com.guli.edu.service.ChapterService;
import com.guli.edu.service.CourseService;
import com.guli.edu.vo.ChapterVo;
import com.guli.edu.vo.CourseWebVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Frank
 * @since 2019-04-15
 */
@CrossOrigin
@Api(description="课程模块")
@RestController
@RequestMapping("/edu/course")
public class CourseController {
    @Autowired
    private CourseService courseService;

    @Autowired
    private ChapterService chapterService;

    @ApiOperation(value = "分页课程列表")
    @GetMapping(value = "{page}/{limit}")
    public R pageList(
            @ApiParam(value = "当前页码", name = "page",required = true)
            @PathVariable("page") long page,
            @ApiParam(value = "每页记录", name = "limit", required = true)
            @PathVariable("limit") long limit
    ){

        Page<Course> coursePageParam = new Page<>(page, limit);
        Map<String, Object> map =  courseService.pageListWeb(coursePageParam);

        return R.ok().data(map);

    }

    @ApiOperation(value = "根据Id获取课程信息")
    @GetMapping("{courseId}")
    public R getById(
            @ApiParam(name = "courseId", value = "课程ID", required = true)
            @PathVariable String courseId){

        CourseWebVo courseWebVo = courseService.selectInfoWebById(courseId);

        //查询当前课程的章节信息
        List<ChapterVo> chapterVoList = chapterService.nestedList(courseId);
        return R.ok().data("course", courseWebVo).data("chapterVoList", chapterVoList);
    }
}

